package com.ms.game.slot.base;

import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * server額外處理盤面結果的紀錄
 * ex: 必中紅利、強制不給分等等
 */
public abstract class ScoreInfoOperateRecord {
	
	private static final Log log = LogFactory.getLog(ScoreInfoOperateRecord.class);
	
	//提供給recordOperate方法的操作參數
	/** 初始化 */
	public static final int RESET = 0;
	/** 設定 主盤面是否使用零分 */
	public static final int SET_MAIN_GAME_NONE_SCORE = 1;
	/** 設定 主盤面是否強制進紅利遊戲 */
	public static final int SET_MAIN_GAME_TO_BONUS = 2;
	/** 主盤面計算次數+1 */
	public static final int ADD_MAIN_GAME_ROLL_TIMES = 3;
	/** 設定 免費遊戲開始使用沒得分的局數 */
	public static final int SET_FREE_GAME_NONE_SCORE_ROUND = 4;
	/** 設定 免費遊戲是否使用必中盤面 */
	public static final int SET_FREE_GAME_USE_MUST_BINGO = 5;
	/** 設定 免費遊戲是否使用亂序(一般用在free spin類型) */
	public static final int SET_FREE_GAME_SHUFFLE = 6;
	/** 增加該局免費遊戲的計算次數 */
	public static final int ADD_FREE_GAME_ROLL_TIMES = 7;
	/** 設定 免費遊戲的禁制金額 */
	public static final int SET_FREE_GAME_LIMIT_MONEY = 8;
	

	/** 主盤面是否有強制設定零分 */
	private boolean isMainGameSetNoneScore;
	
	/** 主盤面是否有強制進紅利遊戲 */
	private boolean isMainGameForceToBonus;
	
	/** 主盤面計算次數 */
	private int mainGameRollTimes;
	
	/** 免費遊戲開始設定零分的局(0=第一局) */
	private int freeGameSetNoneScoreRound = -1;
	
	/** 免費遊戲是否使用了必中盤面 */
	private boolean isFreeGameUseMustBingoScreen;
	
	/** 免費遊戲是否有亂序過盤面 */
	private boolean isFreeGameShuffleOrder;
	
	/** 免費遊戲計算次數(key:盤面IDX，非盤面一律0) */
	private HashMap<Integer, Integer> freeGameRollTimes;
	
	/** 免費遊戲的限制金額 */
	private int freeGameLimitMoney = 0;
	
	/**
	 * 初始化
	 */
	private void init(){
		isMainGameSetNoneScore = false;
		isMainGameForceToBonus = false;
		mainGameRollTimes = 0;
		freeGameSetNoneScoreRound = -1;
		isFreeGameUseMustBingoScreen = false;
		isFreeGameShuffleOrder = false;
		freeGameRollTimes = new HashMap<Integer, Integer>();
		freeGameLimitMoney = 0;
	}

	/**
	 * 紀錄操作
	 * @param type 類型
	 * @param params 提供的參數
	 */
	public void recordOperate(int type, Object ... params){
		try {
			switch (type) {
			case RESET:{
				init();
				break;
			}
			case SET_MAIN_GAME_NONE_SCORE:{
				setMainGameSetNoneScore((boolean) params[0]);
				break;
			}
				
			case SET_MAIN_GAME_TO_BONUS:{
				setMainGameForceToBonus((boolean) params[0]);
				break;
			}
				
			case ADD_MAIN_GAME_ROLL_TIMES:{
				addMainGameRollTimes();
				break;
			}
				
			case SET_FREE_GAME_NONE_SCORE_ROUND:{
				setFreeGameSetNoneScoreRound((int) params[0]);
				break;
			}
				
			case SET_FREE_GAME_USE_MUST_BINGO:{
				setFreeGameUseMustBingoScreen((boolean) params[0]);
				break;
			}
				
			case SET_FREE_GAME_SHUFFLE:{
				setFreeGameShuffleOrder((boolean) params[0]);
				break;
			}
				
			case ADD_FREE_GAME_ROLL_TIMES:{
				addFreeGameRollTimes((int) params[0]);
				break;
			}
			case SET_FREE_GAME_LIMIT_MONEY:{
				setFreeGameLimitMoney((int) params[0]);
				break;
			}
				
			default:
				break;
			}
			
		} catch (Exception e) {
			if(log.isErrorEnabled()){
				log.error("recordOperate fail", e);
			}
			return;
		}
		
		try {
			recordOperateExtand(type, params);	
		} catch (Exception e) {
			if(log.isErrorEnabled()){
				log.error("recordOperateExtand fail", e);
			}
		}
		
	}

	/**
	 * 提供各遊戲繼承改寫<br>
	 * 自定義的type請不要重複，並且從1000之後開始
	 * @param type
	 * @param params
	 */
	public abstract void recordOperateExtand(int type, Object ...params);
	
	private void addMainGameRollTimes(){
		mainGameRollTimes ++;
	}
	
	private void addFreeGameRollTimes(int round){
		if(round < 0){
			return;
		}
		
		if(freeGameRollTimes.containsKey(round)){
			int times = freeGameRollTimes.get(round);
			freeGameRollTimes.put(round, times + 1);
		}
		else{
			freeGameRollTimes.put(round, 1);
		}
	}
	
	@Override
	public String toString() {
		StringBuilder sBuilder = new StringBuilder();
		//mainGame部分
		sBuilder.append("[M]NoneScore=" + isMainGameSetNoneScore + "|");
		sBuilder.append("[M]ForceBonus=" + isMainGameForceToBonus + "|");
		sBuilder.append("[M]RollTimes=" + mainGameRollTimes + "|");
		
		//freeGame部分
		sBuilder.append("[F]NoneScoreRound=" + freeGameSetNoneScoreRound + "|");
		sBuilder.append("[F]MustBingo=" + isFreeGameUseMustBingoScreen + "|");
		sBuilder.append("[F]Shuffle=" + isFreeGameShuffleOrder + "|");
		sBuilder.append("[F]RollTimes=" + freeGameRollTimes.toString() + "|");
		sBuilder.append("[F]LimitMoney=" + freeGameLimitMoney + "|");
		
		//各繼承的內容
		String extand = toStringExtand();
		if(extand != null){
			sBuilder.append(extand);
		}
		
		return sBuilder.toString();
	}
	
	/**
	 * 提供給各繼承改寫，用來輸出自己的紀錄
	 */
	public abstract String toStringExtand();

	
	
	
	
	
	public boolean isMainGameSetNoneScore() {
		return isMainGameSetNoneScore;
	}

	public void setMainGameSetNoneScore(boolean isMainGameSetNoneScore) {
		this.isMainGameSetNoneScore = isMainGameSetNoneScore;
	}

	public boolean isMainGameForceToBonus() {
		return isMainGameForceToBonus;
	}

	public void setMainGameForceToBonus(boolean isMainGameForceToBonus) {
		this.isMainGameForceToBonus = isMainGameForceToBonus;
	}

	public int getMainGameRollTimes() {
		return mainGameRollTimes;
	}

	public void setMainGameRollTimes(int mainGameRollTimes) {
		this.mainGameRollTimes = mainGameRollTimes;
	}

	public int getFreeGameSetNoneScoreRound() {
		return freeGameSetNoneScoreRound;
	}

	public void setFreeGameSetNoneScoreRound(int freeGameSetNoneScoreRound) {
		this.freeGameSetNoneScoreRound = freeGameSetNoneScoreRound;
	}

	public boolean isFreeGameUseMustBingoScreen() {
		return isFreeGameUseMustBingoScreen;
	}

	public void setFreeGameUseMustBingoScreen(boolean isFreeGameUseMustBingoScreen) {
		this.isFreeGameUseMustBingoScreen = isFreeGameUseMustBingoScreen;
	}

	public boolean isFreeGameShuffleOrder() {
		return isFreeGameShuffleOrder;
	}

	public void setFreeGameShuffleOrder(boolean isFreeGameShuffleOrder) {
		this.isFreeGameShuffleOrder = isFreeGameShuffleOrder;
	}

	public HashMap<Integer, Integer> getFreeGameRollTimes() {
		return freeGameRollTimes;
	}

	public void setFreeGameRollTimes(HashMap<Integer, Integer> freeGameRollTimes) {
		this.freeGameRollTimes = freeGameRollTimes;
	}

	public int getFreeGameLimitMoney() {
		return freeGameLimitMoney;
	}

	public void setFreeGameLimitMoney(int freeGameLimitMoney) {
		this.freeGameLimitMoney = freeGameLimitMoney;
	}
	
}